<?php

namespace app\api\model;

use think\Model;

class Createroom extends Model
{   

    protected $table = "t_av_room";
    protected $pk    = 'id';


    public function createRoom($data)
    {
        return $this->allowField(true)->save();
    }

    public function getClassdetail($id)
    {
        $info['class_info'] = $this->where('id',$id)->field('title,start_time as startTime, end_time as endTime, createTime,s tatus as classStatus, realStarttime,realEndtime')->find(); // 未完成，差record 是否录制回放 record_time 0录制时长

        $info['attend_info'] = Db('class_detail')
                                ->alias('c')
                                ->join('account a','a.uid = c.uid')
                                ->where('c.class_id',$id)
                                ->field('c.uid, a.userType as role, c.IP, c.com_time as onLineTime, c.out_time as offLineTime, c.terminal as joinType')
                                ->select();
        return $info;
    }

    public function getreport($id,$report)
    {
        switch ($report) {
            case 'attend':
                $res = [];
                $info = Db('class_detail')->where('class_id',$id)->field('uid,com_time,out_time')->select();
                foreach ($info as $k => $v) {
                    $res[$k]['name'] = $v['uid'];
                    $res[$k]['inTheClassTime'] = $v['com_time'];
                    $time = $v['out_time'] - $v['com_time'];
                    $res[$k]['theClassTime'] = date('H:i:s',$time);
                }

                return $res;
                break;

            case 'pandect':
                $info = $this->where('id',$id)->field('uid as teacher,title as course,createTime as schoolTime')->find();
                $info['reportDate'] = time();
                $info['attendance'] = $info['attendanceNumber'] = Db('class_detail')->where('class_id',$id)->count();

                return $info;
                break;
            
            case 'video':
               
                break;
            
            default:
                $res = [];
                $info = Db('class_detail')->where('class_id',$id)->field('uid,com_time,out_time')->select();
                foreach ($info as $k => $v) {
                    $res[$k]['name'] = $v['uid'];
                    $res[$k]['inTheClassTime'] = $v['com_time'];
                    $time = $v['out_time'] - $v['com_time'];
                    $res[$k]['theClassTime'] = date('H:i:s',$time);
                }

                $info = $this->where('id',$id)->field('uid as teacher,title as course,createTime as schoolTime')->find();
                $info['reportDate'] = time();
                $info['attendance'] = $info['attendanceNumber'] = Db('class_detail')->where('class_id',$id)->count();

                $list['attend'] = $info;
                $list['pandect'] = $res;

                return $list;

                break;
        }
    }

    // 获取登录时长数据
    public function getCharge($id)
    {
        $info = Db('class_detail')->where('id',$id)->field('uid,com_time,out_time')->select();
        foreach ($info as $k => $v) {
            $info[$k]['totaitime'] = ( $v['out_time'] - $v['com_time'] ) / 60;
        }

        return $info;
    }
}